package com.fau.cm.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.fau.cm.entity.MedicalAdmin;
import com.fau.cm.entity.MedicalEnterpriseExtension;
import com.fau.cm.entity.MedicalEnterpriseQueue;
import com.fau.cm.service.IAlternatelyManagerService;

@Service
@Transactional(readOnly = true)
public class AlternatelyManagerService extends BaseService implements
		IAlternatelyManagerService {

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void loginExt(String username, String extenNumId, String queue) {
		this.iBaseDao.updateHql("update MedicalAdmin a set a.phoneNumber=? where a.phoneNumber=? ", "0", extenNumId);
		this.iBaseDao.updateHql("update MedicalAdmin a set a.phoneNumber=? where a.username=? ", extenNumId, username);
		String queueNum = queue + ",";
		this.iBaseDao.updateHql("update MedicalEnterpriseExtension a set a.jobNum=?, a.queue=? where a.extension=? ", username, queueNum, extenNumId);
	}
	
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void loginOutExt(String username, String extenNumId, String queue) {
		this.iBaseDao.updateHql("update MedicalAdmin a set a.phoneNumber=? where a.username=? ", "0", username);
		this.iBaseDao.updateHql("update MedicalEnterpriseExtension a set a.jobNum=?, a.queue=? where a.extension=? ", "0", "0", extenNumId);
	}
	
	@Override
	public String findExtQueue(MedicalAdmin admin) {
		MedicalEnterpriseExtension bean = (MedicalEnterpriseExtension) this.iBaseDao.getObjectByHql("from MedicalEnterpriseExtension a where a.jobNum=? ", admin.getUsername());
		StringBuilder extQueue = new StringBuilder();
		if(null != bean) {
			extQueue.append(bean.getExtension());
			extQueue.append("|");
			extQueue.append(bean.getQueue().substring(0, bean.getQueue().lastIndexOf(",")));
		}
		return extQueue.toString();
	}

	@SuppressWarnings("unchecked")
	@Override
	public String findExtension(String eid) {
		List<MedicalEnterpriseExtension> extensionList = this.iBaseDao.getBySql("from MedicalEnterpriseExtension a where a.eid=? ", null, eid);
		
		StringBuilder eNum = new StringBuilder();
		for(MedicalEnterpriseExtension extension : extensionList) {
			eNum.append(extension.getExtension());
			eNum.append(",");
		}
		if(StringUtils.isNotBlank(eNum)) {
			eNum.deleteCharAt(eNum.lastIndexOf(","));
		}
		return eNum.toString();
	}

	@Override
	public List<Object> getExtensionCall(String extension) {
		
		List<Object> list = new ArrayList<Object>();
		// 呼入数
		int callerCount = this.iBaseDao.getIntCount("select count(*) from MedicalCallCaller a where a.phoneExtNum=? ", extension);
		list.add(callerCount);
		// 呼入时长
		Long callLength = (Long) this.iBaseDao.getObjectByHql("select sum(a.callLength) from MedicalCallCaller a where a.phoneExtNum=? ", extension);
		list.add(callLength);
		
		// 呼出数
		int exhale = this.iBaseDao.getIntCount("select count(*) from MedicalCallExhale a where a.extensionNumber=? ", extension);
		list.add(exhale);
		// 呼出时长
		Long callDuration = (Long) this.iBaseDao.getObjectByHql("select sum(a.callDuration) from MedicalCallExhale a where a.extensionNumber=? ", extension);
		list.add(callDuration);
		
		// 坐席
		MedicalEnterpriseExtension bean = (MedicalEnterpriseExtension) this.iBaseDao.getObjectByHql("from MedicalEnterpriseExtension a where a.extension=? ", extension);
		list.add(bean.getJobNum());
		
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MedicalEnterpriseExtension> findNoExtension(String queueName, String eid) {
		List<MedicalEnterpriseExtension> extensionList = this.iBaseDao.getBySql("from MedicalEnterpriseExtension a where a.eid=? ", null, eid);
		
		Iterator<MedicalEnterpriseExtension> extensionIter = extensionList.iterator();
		while(extensionIter.hasNext()) {
			MedicalEnterpriseExtension extension = extensionIter.next();
			if(StringUtils.isNotBlank(extension.getQueue()) && extension.getQueue().indexOf(queueName) != -1) {
				extensionIter.remove();
			}
		}
		return extensionList;
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void addExtension(String extension, String queueName) {
		String extensionArray[] = extension.split(",");
		for(String eNum : extensionArray) {
			MedicalEnterpriseExtension bean = (MedicalEnterpriseExtension) this.iBaseDao.getObjectByHql("from MedicalEnterpriseExtension a where a.extension=? ", eNum);
			if(StringUtils.isNotBlank(bean.getQueue())) {
				bean.setQueue(bean.getQueue() + "," + queueName);
			} else {
				bean.setQueue(queueName);
			}
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void removeExtension(String extension, String queueName) {
		MedicalEnterpriseExtension bean = (MedicalEnterpriseExtension) this.iBaseDao.getObjectByHql("from MedicalEnterpriseExtension a where a.extension=? ", extension);
		String oldQueue = bean.getQueue();
		String newQueue = oldQueue.replace(queueName + ",", "");
		if(newQueue.length() == 0) {
			newQueue = "0";
			bean.setJobNum("0");
			this.iBaseDao.updateHql("update MedicalAdmin a set a.phoneNumber=? where a.phoneNumber=? ", "0", extension);
		}
		bean.setQueue(newQueue);
	}

	@Override
	public String findCurExtension(String userId) {
		MedicalAdmin admin = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, userId);
		return admin.getPhoneNumber();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MedicalEnterpriseQueue> findAllQueue(MedicalAdmin admin) {
		return this.iBaseDao.getBySql("from MedicalEnterpriseQueue a where a.eid=? ", null, admin.getEnterpriseId());
	}

	@Override
	public MedicalAdmin findExtensionOnline(String extension) {
		MedicalAdmin admin = (MedicalAdmin) this.iBaseDao.getObjectByHql("from MedicalAdmin a where a.phoneNumber=? ", extension);
		return admin;
	}

}
